<template>
  <div class="detail-container">
    <h3>中医百科详情</h3>
    <el-card class="detail-card">
      <div class="detail-item">
        <label>标题:</label>
        <span>{{ detail.title }}</span>
      </div>
      <div class="detail-item">
        <label>分类:</label>
        <span>{{ detail.category }}</span>
      </div>
      <div class="detail-item">
        <label>内容:</label>
        <div class="content-display" v-html="detail.content"></div>
      </div>
      <div class="detail-footer">
        <el-button type="primary" @click="goBack">返回</el-button>
        <el-button :type="detail.collected ? 'danger' : 'primary'" @click="toggleCollect">
          {{ detail.collected ? '取消收藏' : '收藏' }}
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getTcmKnowledgeDetailApi, toggleCollectApi } from '../api/tcmEncyclopedia'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const detail = ref({})

const goBack = () => {
  router.back()
}

const toggleCollect = async () => {
  try {
    await toggleCollectApi(detail.value.id)
    detail.value.collected = !detail.value.collected
    ElMessage.success(detail.value.collected ? '收藏成功' : '取消收藏成功')
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

onMounted(async () => {
  const id = route.params.id
  if (id) {
    try {
      const res = await getTcmKnowledgeDetailApi(id)
      detail.value = res.data
    } catch (error) {
      ElMessage.error('获取详情失败')
    }
  }
})
</script>

<style scoped>
.detail-container {
  padding: 16px;
  max-width: 800px;
  margin: 0 auto;
}

.detail-card {
  margin-top: 20px;
}

.detail-item {
  margin-bottom: 15px;
}

.detail-item label {
  font-weight: bold;
  margin-right: 10px;
  color: #606266;
}

.content-display {
  margin-top: 10px;
  padding: 10px;
  background-color: #f9f9f9;
  border-left: 3px solid #409EFF;
  color: #303133;
  line-height: 1.8;
}

.detail-footer {
  margin-top: 20px;
  text-align: right;
}
</style> 